<template>
<h1>注册页面</h1>
  <el-card style="width: 500px;height: 300px;margin: 0 auto">
    <el-form label-width="100px">
      <el-form-item label="用户名">
        <el-input placeholder="请输入用户名" v-model="user.username"></el-input>
      </el-form-item>
      <el-form-item label="密码">
        <el-input placeholder="请输入密码" type="password"
                  v-model="user.password"></el-input>
      </el-form-item>
      <el-form-item label="昵称">
        <el-input placeholder="请输入昵称" v-model="user.nickname"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="reg()">注册</el-button>
      </el-form-item>
    </el-form>
  </el-card>
</template>

<script setup>
import {ref} from "vue";
import axios from 'axios'
import {ElMessage} from 'element-plus'
import router from "@/router";
import qs from 'qs';
const user = ref({username:"",password:"",nickname:""});
const reg = ()=>{
  //将JS对象转成查询字符串格式,服务器接收数据时就不用使用@RequestBody注解
  // http://localhost:8080/reg?name=tom&age=18 此地址中?后的内容就是查询字符串格式
  //username=tom&password=123456&nickname=%E6%B1%A4%E5%A7%86
  let data = qs.stringify(user.value);
  console.log(data);
  //发出注册请求
  axios.post('http://localhost:8080/v1/users/reg',data)
    .then((response)=>{
      //响应后执行此处代码  response里面装着响应内容 response.data得到服务器响应的数据
      if (response.data.code==2001){
        ElMessage.success("注册成功!");
        //跳转到首页
        router.push('/');
      }else{
        ElMessage.error(response.data.msg);//弹出服务器响应的状态信息
      }
    })
}
</script>

<style scoped>

</style>